<template>
    <el-table
    :data="oList"
    border
    style="width: 100%">
    <el-table-column
      fixed
      prop="info.oid"
      label="订单号"
      >
    </el-table-column>
    <el-table-column
      prop="info.stime"
      label="创建时间"
      >
    </el-table-column>
    <el-table-column
      prop="info.etime"
      label="完成时间"
      >
    </el-table-column>
    <el-table-column
      prop="info.bname"
      label="买家"
      >
    </el-table-column>
    <el-table-column
      prop="glist[0].name"
      label="商品名称"
      >
    </el-table-column>
    <el-table-column
    :formatter="formatterType"
      prop="info.ostate"
      label="订单状态"
     >
    </el-table-column>
    <el-table-column
    :formatter="formatterType2"
      prop="info.otype"
      label="订单类型"
     >
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作"
      >
      <template slot-scope="scope">
        <div v-if="scope.row.info.ostate=='0'">
          <el-button @click="handleGrade(scope.row,'2')" type="text" size="small">确认订单</el-button>
          <el-button @click="sure(scope.row)" type="text" size="small">查看详情</el-button>
        </div>
        <div v-else-if="scope.row.info.ostate=='2'">
          <el-button @click="handleGrade(scope.row,'3')" type="text" size="small">备货完成</el-button>
          <el-button @click="sure(scope.row)" type="text" size="small">查看详情</el-button>
        </div>
        <div v-else-if="scope.row.info.ostate=='3'">
          <el-button @click="handleGrade(scope.row,'4')" type="text" size="small">开始发货</el-button>
          <el-button @click="sure(scope.row)" type="text" size="small">查看详情</el-button>
        </div>
        <div v-else-if="scope.row.info.ostate=='4'">
          <el-button @click="finishGrade(scope.row)" type="text" size="small">交易完成</el-button>
          <el-button @click="handleGrade(scope.row,'-1')" type="text" size="small">取消订单</el-button>
          <el-button @click="sure(scope.row)" type="text" size="small">查看详情</el-button>
      </div>
      <div v-else>
          <el-button @click="sure(scope.row)" type="text" size="small">查看详情</el-button>
      </div>
      </template>
    </el-table-column>
  </el-table>
  </template>
  <script>
  export default {
      inject:['reload'],
      name:'MyOrder',
      data(){
          return{
            orderList: []
          }
      },
      methods:{
        getOrderList(){
            this.$axios.get('/api/getAllOrder').then(res =>{
                this.orderList=res.data.data
              }).catch(error =>{
               // console.log('提交失败')
                //console.log(error )
              })
        },
        formatterType(row){
          switch(row.info.ostate){
            case 0:
              return '客户下单'
            case 1:
              return '交易完成'
            case 2:
              return '商家确认'
            case 3:
              return '备货完成'
            case 4:
              return '开始发货'
            case -1:
              return '取消订单'
            default:
              return ''
          }
      },
      formatterType2(row){
          switch(row.info.otype){
            case 0:
              return '购买'
            case 1:
              return '租借'
            default:
              return ''
          }
        },
        sure(row){
          this.$router.push({
              name:'ord',
              query:{
                oid:row.info.oid
              }
          })
        },
        async finishGrade(row){
          const oid=row.info.oid;
          const gid=row.info.gid;
          const gq=0
          this.$axios.get("/api/getGoodByGid",{params:{gid:gid}}).then(res =>{
            //console.log(res.data.data)
              gq=res.data.data.gquantity
              }).catch(error =>{
                //console.log(error)
          })
          if(gq==0){
            const gstate='2';
            await this.$store.dispatch("gstateUpdate",{gstate,gid});
          }else{
            const gstate='1';
            await this.$store.dispatch("gstateUpdate",{gstate,gid});
          }
          this.$axios.get("/api/finishOrder",{params:{oid:oid}}).then(res =>{
               this.reload()
              }).catch(error =>{
                //console.log(error)
          })
          
        },
        async handleGrade(row,number){
          //console.log(row.info.gnum)
          const ostate=number;
          const oid=row.info.oid;
          // if(number==='2'){
          //   //商品下架
          //   const gstate='0';
          //   const gid=row.info.gid;
          //   await this.$axios.post("/api/updateOrderState",{},{
          //             headers:{
          //                   'satoken':localStorage.getItem("TOKEN")
          //             },
          //             params:{
          //               'oid':oid
          //             }
          //           }).then(res =>{
          //             console.log(res)
          //               }).catch(error =>{
          //                 //console.log(error)
          //               })
          // }else if(number==='-1'){
          //   //商品恢复上架
          //   const gstate='1';
          //   const gid=row.info.gid;
          //   await this.$store.dispatch("gstateUpdate",{gstate,gid});
          // }else if(number==='1'){
          //   //商品状态变为下架
          //   const gstate='2';
          //   const gid=row.info.gid;
          //   await this.$store.dispatch("gstateUpdate",{gstate,gid});
          // }
          this.$axios.post("/api/updateOrderState",{},{
              headers:{
                      'satoken':localStorage.getItem("TOKEN")
                    },
              params:{
                'oid':oid,
                'ostate':ostate
            }
          }).then(res =>{
              console.log(res.data)
               //this.reload()
              }).catch(error =>{
                //console.log(error)
              })
          },
      },
      computed:{
        oList(){
        const  lt= JSON.parse(JSON.stringify(this.orderList));
        console.log(lt)
        return lt
        },
        
      },
      mounted(){
        this.getOrderList()
      }
  }
  </script>
  <style>
  
  </style>